using System.Collections;
using System.Collections.Generic;
using I2.Loc;
using UnityEngine;

public class GameTypeSelector : MonoBehaviour
{
	[SerializeField]
	private CellChooseTransport transportButton;

	[SerializeField]
	private CellChooseDistrict districtButton;

	[SerializeField]
	private TransportButtonController transportController;

	[SerializeField]
	private SelectableButtonController districtController;

	[SerializeField]
	private UIScrollView transportScroll;

	[SerializeField]
	private UIScrollView districtScroll;

	[SerializeField]
	private GameObject ScrollBar;

	[SerializeField]
	private GameObject playerPrevie;

	[SerializeField]
	private GameObject teamWindow;

	[SerializeField]
	private GameObject singleWindow;

	[SerializeField]
	private GameObject freeWindow;

	[SerializeField]
	private GameObject tankWindow;

	[SerializeField]
	private GameObject racesWindow;

	[SerializeField]
	private GameObject zombieWindow;

	public PreviewTransportController transportPreview;

	public GameTypeSelectorCarInfo carInfo;

	public SelectableButton defaultInset;

	private string[] idFreeCars = new string[0];

	private string[] idFreeTanks = new string[0];

	private void Awake()
	{
		transportButton.gameObject.SetActive(false);
		districtButton.gameObject.SetActive(false);
	}

	public void Show()
	{
		if (CheckVersion.instance != null && !CheckVersion.actualVersion)
		{
			CheckVersion.instance.ShowUpdate();
			return;
		}
		base.gameObject.SetActive(true);
		defaultInset.Select(true);
	}

	public void Exit()
	{
		base.gameObject.SetActive(false);
	}

	private void HideAllWindows()
	{
		transportController.gameObject.SetActive(false);
		districtController.gameObject.SetActive(false);
		teamWindow.SetActive(false);
		singleWindow.SetActive(false);
		freeWindow.SetActive(false);
		tankWindow.SetActive(false);
		racesWindow.SetActive(false);
		zombieWindow.SetActive(false);
		transportPreview.DestroyCar();
		carInfo.ShowMiniInfoPanel(null);
	}

	public void InsetRace()
	{
		HideAllWindows();
		transportController.gameObject.SetActive(true);
		racesWindow.gameObject.SetActive(true);
		List<Product> listPurchasetCar = MarketController.Instance.GetListPurchasetCar();
		string[] array = idFreeCars;
		foreach (string id in array)
		{
			listPurchasetCar.Add(MarketController.Instance.GetProduct(id));
		}
		StartCoroutine(LoadTransporInScroll(listPurchasetCar, TypeMatchForButton.Car));
		playerPrevie.SetActive(false);
		ScrollBar.SetActive(false);
	}

	public void InsetTank()
	{
		HideAllWindows();
		transportController.gameObject.SetActive(true);
		tankWindow.gameObject.SetActive(true);
		List<Product> listPurchasetTank = MarketController.Instance.GetListPurchasetTank();
		string[] array = idFreeTanks;
		foreach (string id in array)
		{
			listPurchasetTank.Add(MarketController.Instance.GetProduct(id));
		}
		StartCoroutine(LoadTransporInScroll(listPurchasetTank, TypeMatchForButton.Tank));
		playerPrevie.SetActive(false);
		ScrollBar.SetActive(false);
	}

	public void InsetFree()
	{
		HideAllWindows();
		districtController.gameObject.SetActive(true);
		freeWindow.gameObject.SetActive(true);
		StartCoroutine(LoadMapInScroll(SpawnPointChief.listNameArea, TypeMatchForButton.Map));
		playerPrevie.SetActive(true);
		ScrollBar.SetActive(true);
	}

	public void InsetSingle()
	{
		HideAllWindows();
		districtController.gameObject.SetActive(true);
		singleWindow.gameObject.SetActive(true);
		StartCoroutine(LoadMapInScroll(SpawnPointChief.listNameArea, TypeMatchForButton.Map));
		playerPrevie.SetActive(true);
		ScrollBar.SetActive(true);
	}

	public void InsetTeam()
	{
		HideAllWindows();
		teamWindow.gameObject.SetActive(true);
		ClearScrolls();
		playerPrevie.SetActive(true);
		ScrollBar.SetActive(false);
	}

	public void InsetZombie()
	{
		HideAllWindows();
		zombieWindow.gameObject.SetActive(true);
		ClearScrolls();
		playerPrevie.SetActive(true);
		ScrollBar.SetActive(false);
	}

	public void StartRace()
	{
		if (CheckVersion.instance != null && !CheckVersion.actualVersion)
		{
			CheckVersion.instance.ShowUpdate();
			return;
		}
		if (settings.RaceCarName.Equals(string.Empty))
		{
			settings.RaceCarName = "_car_dodje_ChelendjerRace";
		}
		if (!PhotonNetwork.connected)
		{
			controllerMenu.thisScript.ShowNoInternet();
		}
		controllerConnectPhoton.thisScript.connectRandomRoom(string.Empty, TypeGame.race);
	}

	public void StartTankBattle()
	{
		if (CheckVersion.instance != null && !CheckVersion.actualVersion)
		{
			CheckVersion.instance.ShowUpdate();
			return;
		}
		if (settings.SelectedTankName.Equals(string.Empty))
		{
			settings.SelectedTankName = "_CarTankFree";
		}
		controllerConnectPhoton.thisScript.connectRandomRoom(string.Empty, TypeGame.tanks);
		if (!PhotonNetwork.connected)
		{
			controllerMenu.thisScript.ShowNoInternet();
		}
	}

	public void StartTeamBattle()
	{
		if (CheckVersion.instance != null && !CheckVersion.actualVersion)
		{
			CheckVersion.instance.ShowUpdate();
			return;
		}
		controllerConnectPhoton.thisScript.connectRandomRoom(string.Empty, TypeGame.TDM);
		Debug.Log("Start Team Battle");
		if (!PhotonNetwork.connected)
		{
			controllerMenu.thisScript.ShowNoInternet();
		}
	}

	public void StartDeathmatch()
	{
		Debug.Log("Start Deathmatch");
	}

	public void StartSingle()
	{
		if (CheckVersion.instance != null && !CheckVersion.actualVersion)
		{
			CheckVersion.instance.ShowUpdate();
		}
		else
		{
			controllerMenu.thisScript.playOfflineMode();
		}
	}

	public void StartFree()
	{
		if (CheckVersion.instance != null && !CheckVersion.actualVersion)
		{
			CheckVersion.instance.ShowUpdate();
			return;
		}
		controllerConnectPhoton.thisScript.connectRandomRoom("Level1");
		if (!PhotonNetwork.connected)
		{
			controllerMenu.thisScript.ShowNoInternet();
		}
	}

	public void StartZombie()
	{
		if (CheckVersion.instance != null && !CheckVersion.actualVersion)
		{
			CheckVersion.instance.ShowUpdate();
			return;
		}
		controllerConnectPhoton.thisScript.connectRandomRoom(string.Empty, TypeGame.Zombe);
		if (!PhotonNetwork.connected)
		{
			controllerMenu.thisScript.ShowNoInternet();
		}
	}

	public void StartCopsAndRobbers()
	{
		if (CheckVersion.instance != null && !CheckVersion.actualVersion)
		{
			CheckVersion.instance.ShowUpdate();
			return;
		}
		controllerConnectPhoton.thisScript.connectRandomRoom(string.Empty, TypeGame.CopsAndRobbers);
		if (!PhotonNetwork.connected)
		{
			controllerMenu.thisScript.ShowNoInternet();
		}
	}

	public void StartSkyBlock()
	{
		if (CheckVersion.instance != null && !CheckVersion.actualVersion)
		{
			CheckVersion.instance.ShowUpdate();
			return;
		}
		controllerConnectPhoton.thisScript.connectRandomRoom(string.Empty, TypeGame.SkyBlock);
		if (!PhotonNetwork.connected)
		{
			controllerMenu.thisScript.ShowNoInternet();
		}
	}

	public void StartRocketLeague()
	{
		if (CheckVersion.instance != null && !CheckVersion.actualVersion)
		{
			CheckVersion.instance.ShowUpdate();
			return;
		}
		controllerConnectPhoton.thisScript.connectRandomRoom(string.Empty, TypeGame.RocketLeague);
		if (!PhotonNetwork.connected)
		{
			controllerMenu.thisScript.ShowNoInternet();
		}
	}

	private void ClearScrolls()
	{
		transportController.ClearAllButtons();
		districtController.ClearAllButtons();
		transportController.grid.repositionNow = true;
		transportController.grid.repositionNow = true;
		transportScroll.ResetPosition();
		districtScroll.ResetPosition();
	}

	private IEnumerator LoadMapInScroll(List<string> listKeyArea, TypeMatchForButton type)
	{
		ClearScrolls();
		foreach (string element in listKeyArea)
		{
			CellChooseDistrict button = Object.Instantiate(districtButton);
			button.gameObject.SetActive(true);
			button._widget.SetActive(false);
			districtController.AddNewButton(button);
			button.transform.SetParent(districtController.grid.transform);
			button.transform.localScale = Vector3.one;
			button.id = element;
			button.typeMatch = type;
			button.selectableButtonController = districtController;
			button.lbName.text = ScriptLocalization.Get("PVP_map_info/" + element);
			button.icon.mainTexture = Resources.Load<Texture>("GameTypeTexture/" + element);
		}
		if (districtController.allButtons[0] != null)
		{
			districtController.allButtons[0].Select(true);
		}
		districtController.grid.repositionNow = true;
		districtController.grid.onReposition = OnRepositionDistricts;
		yield return null;
		districtScroll.ResetPosition();
	}

	private void OnRepositionDistricts()
	{
		districtScroll.ResetPosition();
		foreach (SelectableButton allButton in districtController.allButtons)
		{
			((CellChooseDistrict)allButton)._widget.SetActive(true);
		}
	}

	private IEnumerator LoadTransporInScroll(List<Product> listProduct, TypeMatchForButton type)
	{
		ClearScrolls();
		TransportButtonController controller = transportScroll.GetComponent<TransportButtonController>();
		List<ProductCar> actualCarList = new List<ProductCar>();
		foreach (Product element2 in listProduct)
		{
			if (element2 is ProductCar)
			{
				bool ignored = false;
				ProductCar carP = (ProductCar)element2;
				if (type == TypeMatchForButton.Car && carP.notForRace)
				{
					ignored = true;
				}
				if (!ignored)
				{
					actualCarList.Add(carP);
				}
			}
		}
		actualCarList.Sort((ProductCar a, ProductCar b) => a.sortWeight.CompareTo(b.sortWeight));
		foreach (ProductCar element in actualCarList)
		{
			CellChooseTransport button = Object.Instantiate(transportButton);
			button.gameObject.SetActive(true);
			transportController.AddNewButton(button);
			button.transform.SetParent(transportController.grid.transform);
			button.transform.localScale = Vector3.one;
			button.gameTypeSelector = this;
			button.carProduct = element;
			button.selectableButtonController = transportController;
			button.icon.mainTexture = Resources.Load<Texture2D>("ModeSelektor/" + MarketController.Instance.GetProduct(element.id).texture);
			button.lbName.text = ((!string.IsNullOrEmpty(ScriptLocalization.Get("Market/" + element.id + "_title"))) ? ScriptLocalization.Get("Market/" + element.id + "_title") : "Нет локализации");
			button.typeMatch = type;
		}
		transportController.grid.repositionNow = true;
		transportController.grid.onReposition = controller.Reset;
		yield return null;
	}
}
